﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.IO;


public partial class frmListaDeOrdenesPrestacion : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

      
    }
    protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        String fecha = DateTime.Now.ToString("dd/MM/yyyy");
        string imagepath = Server.MapPath("Img");


        //if ((TextBox2.Text.Equals("")) && (TextBox3.Text.Equals("")))
        if ((!CheckBox1.Checked)&&(!CheckBox2.Checked))
        {

            Calendar1.Visible = false;
            Calendar2.Visible = false;


            int presup = 0;
            int total = 0;

            this.DSOrdenPrestacion.DataSourceMode = SqlDataSourceMode.DataReader;
            SqlDataReader datos = (SqlDataReader)this.DSOrdenPrestacion.Select(DataSourceSelectArguments.Empty);

            FileStream fs = new FileStream(Server.MapPath("~/") + "ORDENESPRESTACION.pdf", FileMode.Create, FileAccess.Write, FileShare.None);
            Rectangle rec3 = new Rectangle(PageSize.A4);
            Document doc = new Document(rec3, 5, 5, 5, 5);
            PdfWriter writer = PdfWriter.GetInstance(doc, fs);
            iTextSharp.text.Font fontTitle = FontFactory.GetFont("Arial", 18, iTextSharp.text.Font.BOLD, BaseColor.BLACK);
            iTextSharp.text.Font fontHead = FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.BOLD, BaseColor.BLACK);
            iTextSharp.text.Font fontBody = FontFactory.GetFont("Arial", 10, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);

            Console.WriteLine("");

            Paragraph titulo = new Paragraph("LISTADO DE ORDENES DE PRESTACION AL " + fecha, fontTitle);
            titulo.Alignment = Element.ALIGN_JUSTIFIED;

            doc.Open();
            iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(imagepath + "/logoaf.png");
            doc.Add(logo);
            doc.Add(titulo);
            doc.Add(new Paragraph("  "));


            iTextSharp.text.pdf.PdfPTable table = new iTextSharp.text.pdf.PdfPTable(8);
            table.HorizontalAlignment = 0;
            table.TotalWidth = 550f;
            table.LockedWidth = true;
            float[] widths = new float[] { 70f, 70f, 100f, 70f, 60f, 60f, 60f, 60f };
            table.SetWidths(widths);

            PdfPCell hnro = new PdfPCell(new Phrase("NRO.ORDEN", new Font(fontHead)));
            PdfPCell hpresup = new PdfPCell(new Phrase("NRO.PRESUP.", new Font(fontHead)));
            PdfPCell hempresa = new PdfPCell(new Phrase("EMPRESA", new Font(fontHead)));
            PdfPCell hdetalle = new PdfPCell(new Phrase("DETALLE", new Font(fontHead)));
            PdfPCell himporte = new PdfPCell(new Phrase("IMPORTE", new Font(fontHead)));
            PdfPCell hestado = new PdfPCell(new Phrase("ESTADO", new Font(fontHead)));
            PdfPCell hfechaPresup = new PdfPCell(new Phrase("FECHA PRESUP.", new Font(fontHead)));
            PdfPCell hvto = new PdfPCell(new Phrase("VENCIMIENTO", new Font(fontHead)));
            table.AddCell(hnro);
            table.AddCell(hpresup);
            table.AddCell(hempresa);
            table.AddCell(hdetalle);
            table.AddCell(himporte);
            table.AddCell(hestado);
            table.AddCell(hfechaPresup);
            table.AddCell(hvto);


            while (datos.Read())
            {
                String numeroOrden = datos["nroOrdenPrestacionServicio"].ToString();
                String numeroPresup = datos["nroPresupuesto"].ToString();
                String detalle = datos["detalle"].ToString();
                String razon = datos["razonSocial"].ToString();
                String importe = datos["importe"].ToString();
                String estado = datos["descripcionEstado"].ToString();
                String fechaPresup = datos["fechaPresupuesto"].ToString();
                String vto = datos["fechaValidez"].ToString();

                PdfPCell corden = new PdfPCell(new Phrase(numeroOrden, new Font(fontBody)));
                PdfPCell cpresup = new PdfPCell(new Phrase(numeroPresup, new Font(fontBody)));
                PdfPCell cdetalle = new PdfPCell(new Phrase(detalle, new Font(fontBody)));
                PdfPCell crazon = new PdfPCell(new Phrase(razon, new Font(fontBody)));
                PdfPCell cimporte = new PdfPCell(new Phrase("$ " + importe, new Font(fontBody)));
                PdfPCell cestado = new PdfPCell(new Phrase(estado, new Font(fontBody)));
                PdfPCell cfechapresup = new PdfPCell(new Phrase(fechaPresup, new Font(fontBody)));
                PdfPCell cvto = new PdfPCell(new Phrase(vto, new Font(fontBody)));

                table.AddCell(corden);
                table.AddCell(cpresup);
                table.AddCell(crazon);
                table.AddCell(cdetalle);
                table.AddCell(cimporte);
                table.AddCell(cestado);
                table.AddCell(cfechapresup);
                table.AddCell(cvto);
                presup = presup + 1;
                int imp = Convert.ToInt32(importe);
                total = total + imp;


            }

            doc.Add(table);
            doc.Add(new Paragraph("  "));
            doc.Add(new Paragraph("  "));
            doc.Add(new Paragraph("TOTAL DE ORDENES: " + presup));
            doc.Add(new Paragraph("IMPORTE TOTAL: $" + total));
            doc.Close();

            Response.ContentType = "application/pdf";
            Response.AddHeader("Content-Disposition", "attachment;filename=ORDENESPRESTACION.pdf");
            Response.TransmitFile(Server.MapPath("~/") + "ORDENESPRESTACION.pdf");
        }


        else
        {
            if ((CheckBox1.Checked)&&(!CheckBox2.Checked))//Apertura solo por fecha
            {
                
                if (TextBox2.Text.Equals("") || TextBox3.Text.Equals(""))
                    Label14.Text = "Debe Seleccionar un Período Válido";

                else
                {
                    DateTime desde = Convert.ToDateTime(TextBox2.Text);
                    DateTime hasta = Convert.ToDateTime(TextBox3.Text);

                    TimeSpan diff = hasta.Subtract(desde);
                    int dias = diff.Days;

                    if (dias < 0)
                        Label14.Text = "Debe Seleccionar un Período Válido";

                    else
                    {
                        int presup = 0;
                        int total = 0;

                        this.DSOrdenPrestacion.DataSourceMode = SqlDataSourceMode.DataReader;
                        SqlDataReader datos = (SqlDataReader)this.DSOrdenPrestacion.Select(DataSourceSelectArguments.Empty);

                        FileStream fs = new FileStream(Server.MapPath("~/") + "ORDENESPRESTACION.pdf", FileMode.Create, FileAccess.Write, FileShare.None);
                        Rectangle rec3 = new Rectangle(PageSize.A4);
                        Document doc = new Document(rec3, 5, 5, 5, 5);
                        PdfWriter writer = PdfWriter.GetInstance(doc, fs);
                        iTextSharp.text.Font fontTitle = FontFactory.GetFont("Arial", 18, iTextSharp.text.Font.BOLD, BaseColor.BLACK);
                        iTextSharp.text.Font fontHead = FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.BOLD, BaseColor.BLACK);
                        iTextSharp.text.Font fontBody = FontFactory.GetFont("Arial", 10, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);

                        Console.WriteLine("");

                        Paragraph titulo = new Paragraph("LISTADO DE ORDENES DE PRESTACION AL " + fecha, fontTitle);
                        titulo.Alignment = Element.ALIGN_JUSTIFIED;

                        doc.Open();
                        iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(imagepath + "/logoaf.png");
                        doc.Add(logo);
                        doc.Add(titulo);
                        doc.Add(new Paragraph("  "));
                        Paragraph entres = new Paragraph("Entre el " + desde.ToString("dd/MM/yyyy") + " y el " + hasta.ToString("dd/MM/yyyy"), fontHead);
                        doc.Add(new Paragraph(entres));
                        doc.Add(new Paragraph(" "));
                       
                        iTextSharp.text.pdf.PdfPTable table = new iTextSharp.text.pdf.PdfPTable(8);
                        table.HorizontalAlignment = 0;
                        table.TotalWidth = 550f;
                        table.LockedWidth = true;
                        float[] widths = new float[] { 70f, 70f, 100f, 70f, 60f, 60f, 60f, 60f };
                        table.SetWidths(widths);

                        PdfPCell hnro = new PdfPCell(new Phrase("NRO.ORDEN", new Font(fontHead)));
                        PdfPCell hpresup = new PdfPCell(new Phrase("NRO.PRESUP.", new Font(fontHead)));
                        PdfPCell hempresa = new PdfPCell(new Phrase("EMPRESA", new Font(fontHead)));
                        PdfPCell hdetalle = new PdfPCell(new Phrase("DETALLE", new Font(fontHead)));
                        PdfPCell himporte = new PdfPCell(new Phrase("IMPORTE", new Font(fontHead)));
                        PdfPCell hestado = new PdfPCell(new Phrase("ESTADO", new Font(fontHead)));
                        PdfPCell hfechaPresup = new PdfPCell(new Phrase("FECHA PRESUP.", new Font(fontHead)));
                        PdfPCell hvto = new PdfPCell(new Phrase("VENCIMIENTO", new Font(fontHead)));
                        table.AddCell(hnro);
                        table.AddCell(hpresup);
                        table.AddCell(hempresa);
                        table.AddCell(hdetalle);
                        table.AddCell(himporte);
                        table.AddCell(hestado);
                        table.AddCell(hfechaPresup);
                        table.AddCell(hvto);


                        while (datos.Read())
                        {
                            String numeroOrden = datos["nroOrdenPrestacionServicio"].ToString();
                            String numeroPresup = datos["nroPresupuesto"].ToString();
                            String detalle = datos["detalle"].ToString();
                            String razon = datos["razonSocial"].ToString();
                            String importe = datos["importe"].ToString();
                            String estado = datos["descripcionEstado"].ToString();
                            String fechaOrden = datos["fechaOrden"].ToString();
                            String vto = datos["fechaValidez"].ToString();

                            PdfPCell corden = new PdfPCell(new Phrase(numeroOrden, new Font(fontBody)));
                            PdfPCell cpresup = new PdfPCell(new Phrase(numeroPresup, new Font(fontBody)));
                            PdfPCell cdetalle = new PdfPCell(new Phrase(detalle, new Font(fontBody)));
                            PdfPCell crazon = new PdfPCell(new Phrase(razon, new Font(fontBody)));
                            PdfPCell cimporte = new PdfPCell(new Phrase("$ " + importe, new Font(fontBody)));
                            PdfPCell cestado = new PdfPCell(new Phrase(estado, new Font(fontBody)));
                            PdfPCell cfechapresup = new PdfPCell(new Phrase(fechaOrden, new Font(fontBody)));
                            PdfPCell cvto = new PdfPCell(new Phrase(vto, new Font(fontBody)));


                            DateTime entre = Convert.ToDateTime(fechaOrden);

                            if ((desde <= entre) && (entre <= hasta))
                            {


                                table.AddCell(corden);
                                table.AddCell(cpresup);
                                table.AddCell(crazon);
                                table.AddCell(cdetalle);
                                table.AddCell(cimporte);
                                table.AddCell(cestado);
                                table.AddCell(cfechapresup);
                                table.AddCell(cvto);
                                presup = presup + 1;
                                int imp = Convert.ToInt32(importe);
                                total = total + imp;

                            }

                        }

                        doc.Add(table);
                        doc.Add(new Paragraph("  "));
                        doc.Add(new Paragraph("  "));
                        doc.Add(new Paragraph("TOTAL DE ORDENES: " + presup));
                        doc.Add(new Paragraph("IMPORTE TOTAL: $" + total));
                        doc.Close();

                        Response.ContentType = "application/pdf";
                        Response.AddHeader("Content-Disposition", "attachment;filename=ORDENESPRESTACION.pdf");
                        Response.TransmitFile(Server.MapPath("~/") + "ORDENESPRESTACION.pdf");

                    }
                }
           
            }//cierre solo por fecha

            if (CheckBox2.Checked)//Apertura solo por empresa

            { 
                        int presup = 0;
                        int total = 0;


                        this.DSOrdenPrestacion.DataSourceMode = SqlDataSourceMode.DataReader;
                        SqlDataReader datos = (SqlDataReader)this.DSOrdenPrestacion.Select(DataSourceSelectArguments.Empty);

                        FileStream fs = new FileStream(Server.MapPath("~/") + "ORDENESPRESTACION.pdf", FileMode.Create, FileAccess.Write, FileShare.None);
                        Rectangle rec3 = new Rectangle(PageSize.A4);
                        Document doc = new Document(rec3, 5, 5, 5, 5);
                        PdfWriter writer = PdfWriter.GetInstance(doc, fs);
                        iTextSharp.text.Font fontTitle = FontFactory.GetFont("Arial", 18, iTextSharp.text.Font.BOLD, BaseColor.BLACK);
                        iTextSharp.text.Font fontHead = FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.BOLD, BaseColor.BLACK);
                        iTextSharp.text.Font fontBody = FontFactory.GetFont("Arial", 10, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);

                        Console.WriteLine("");

                        Paragraph titulo = new Paragraph("LISTADO DE ORDENES DE PRESTACION AL " + fecha, fontTitle);
                        titulo.Alignment = Element.ALIGN_JUSTIFIED;

                        doc.Open();
                        iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(imagepath + "/logoaf.png");
                        doc.Add(logo);
                        doc.Add(titulo);
                        doc.Add(new Paragraph("  "));
                       



                        iTextSharp.text.pdf.PdfPTable table = new iTextSharp.text.pdf.PdfPTable(8);
                        table.HorizontalAlignment = 0;
                        table.TotalWidth = 550f;
                        table.LockedWidth = true;
                        float[] widths = new float[] { 70f, 70f, 100f, 70f, 60f, 60f, 60f, 60f };
                        table.SetWidths(widths);

                        PdfPCell hnro = new PdfPCell(new Phrase("NRO.ORDEN", new Font(fontHead)));
                        PdfPCell hpresup = new PdfPCell(new Phrase("NRO.PRESUP.", new Font(fontHead)));
                        PdfPCell hempresa = new PdfPCell(new Phrase("EMPRESA", new Font(fontHead)));
                        PdfPCell hdetalle = new PdfPCell(new Phrase("DETALLE", new Font(fontHead)));
                        PdfPCell himporte = new PdfPCell(new Phrase("IMPORTE", new Font(fontHead)));
                        PdfPCell hestado = new PdfPCell(new Phrase("ESTADO", new Font(fontHead)));
                        PdfPCell hfechaPresup = new PdfPCell(new Phrase("FECHA PRESUP.", new Font(fontHead)));
                        PdfPCell hvto = new PdfPCell(new Phrase("VENCIMIENTO", new Font(fontHead)));
                        table.AddCell(hnro);
                        table.AddCell(hpresup);
                        table.AddCell(hempresa);
                        table.AddCell(hdetalle);
                        table.AddCell(himporte);
                        table.AddCell(hestado);
                        table.AddCell(hfechaPresup);
                        table.AddCell(hvto);


                        while (datos.Read())
                        {
                            String numeroOrden = datos["nroOrdenPrestacionServicio"].ToString();
                            String numeroPresup = datos["nroPresupuesto"].ToString();
                            String detalle = datos["detalle"].ToString();
                            String razon = datos["razonSocial"].ToString();
                            String importe = datos["importe"].ToString();
                            String estado = datos["descripcionEstado"].ToString();
                            String fechaOrden = datos["fechaOrden"].ToString();
                            String vto = datos["fechaValidez"].ToString();

                            PdfPCell corden = new PdfPCell(new Phrase(numeroOrden, new Font(fontBody)));
                            PdfPCell cpresup = new PdfPCell(new Phrase(numeroPresup, new Font(fontBody)));
                            PdfPCell cdetalle = new PdfPCell(new Phrase(detalle, new Font(fontBody)));
                            PdfPCell crazon = new PdfPCell(new Phrase(razon, new Font(fontBody)));
                            PdfPCell cimporte = new PdfPCell(new Phrase("$ " + importe, new Font(fontBody)));
                            PdfPCell cestado = new PdfPCell(new Phrase(estado, new Font(fontBody)));
                            PdfPCell cfechapresup = new PdfPCell(new Phrase(fechaOrden, new Font(fontBody)));
                            PdfPCell cvto = new PdfPCell(new Phrase(vto, new Font(fontBody)));

                            Label15.Text = DropDownList1.SelectedItem.ToString();

                            if(Label15.Text.Equals(razon))
                            {
                                table.AddCell(corden);
                                table.AddCell(cpresup);
                                table.AddCell(crazon);
                                table.AddCell(cdetalle);
                                table.AddCell(cimporte);
                                table.AddCell(cestado);
                                table.AddCell(cfechapresup);
                                table.AddCell(cvto);
                                presup = presup + 1;
                                int imp = Convert.ToInt32(importe);
                                total = total + imp;
                            }
                           
                        }

                        doc.Add(table);
                        doc.Add(new Paragraph("  "));
                        doc.Add(new Paragraph("  "));
                        doc.Add(new Paragraph("TOTAL DE ORDENES: " + presup));
                        doc.Add(new Paragraph("IMPORTE TOTAL: $" + total));
                        doc.Close();

                        Response.ContentType = "application/pdf";
                        Response.AddHeader("Content-Disposition", "attachment;filename=ORDENESPRESTACION.pdf");
                        Response.TransmitFile(Server.MapPath("~/") + "ORDENESPRESTACION.pdf");

            
            }
            // Cierre solo por Empresa


            if ((CheckBox1.Checked)&&(CheckBox2.Checked))//Apertura por empresa y fecha
            {

                if (TextBox2.Text.Equals("") || TextBox3.Text.Equals(""))
                    Label14.Text = "Debe Seleccionar un Período Válido";

                else
                {
                    DateTime desde = Convert.ToDateTime(TextBox2.Text);
                    DateTime hasta = Convert.ToDateTime(TextBox3.Text);

                    TimeSpan diff = hasta.Subtract(desde);
                    int dias = diff.Days;

                    if (dias < 0)
                        Label14.Text = "Debe Seleccionar un Período Válido";

                    else
                    {
                         desde = Convert.ToDateTime(TextBox2.Text);
                         hasta = Convert.ToDateTime(TextBox3.Text);

                        int presup = 0;
                        int total = 0;

                        this.DSOrdenPrestacion.DataSourceMode = SqlDataSourceMode.DataReader;
                        SqlDataReader datos = (SqlDataReader)this.DSOrdenPrestacion.Select(DataSourceSelectArguments.Empty);

                        FileStream fs = new FileStream(Server.MapPath("~/") + "ORDENESPRESTACION.pdf", FileMode.Create, FileAccess.Write, FileShare.None);
                        Rectangle rec3 = new Rectangle(PageSize.A4);
                        Document doc = new Document(rec3, 5, 5, 5, 5);
                        PdfWriter writer = PdfWriter.GetInstance(doc, fs);
                        iTextSharp.text.Font fontTitle = FontFactory.GetFont("Arial", 18, iTextSharp.text.Font.BOLD, BaseColor.BLACK);
                        iTextSharp.text.Font fontHead = FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.BOLD, BaseColor.BLACK);
                        iTextSharp.text.Font fontBody = FontFactory.GetFont("Arial", 10, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);

                        Console.WriteLine("");

                        Paragraph titulo = new Paragraph("LISTADO DE ORDENES DE PRESTACION AL " + fecha, fontTitle);
                        titulo.Alignment = Element.ALIGN_JUSTIFIED;

                        doc.Open();
                        iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(imagepath + "/logoaf.png");
                        doc.Add(logo);
                        doc.Add(titulo);
                        doc.Add(new Paragraph("  "));
                        Paragraph entres = new Paragraph("Entre el " + desde.ToString("dd/MM/yyyy") + " y el " + hasta.ToString("dd/MM/yyyy"), fontHead);
                        doc.Add(new Paragraph(entres));
                        doc.Add(new Paragraph(" "));


                        iTextSharp.text.pdf.PdfPTable table = new iTextSharp.text.pdf.PdfPTable(8);
                        table.HorizontalAlignment = 0;
                        table.TotalWidth = 550f;
                        table.LockedWidth = true;
                        float[] widths = new float[] { 70f, 70f, 100f, 70f, 60f, 60f, 60f, 60f };
                        table.SetWidths(widths);

                        PdfPCell hnro = new PdfPCell(new Phrase("NRO.ORDEN", new Font(fontHead)));
                        PdfPCell hpresup = new PdfPCell(new Phrase("NRO.PRESUP.", new Font(fontHead)));
                        PdfPCell hempresa = new PdfPCell(new Phrase("EMPRESA", new Font(fontHead)));
                        PdfPCell hdetalle = new PdfPCell(new Phrase("DETALLE", new Font(fontHead)));
                        PdfPCell himporte = new PdfPCell(new Phrase("IMPORTE", new Font(fontHead)));
                        PdfPCell hestado = new PdfPCell(new Phrase("ESTADO", new Font(fontHead)));
                        PdfPCell hfechaPresup = new PdfPCell(new Phrase("FECHA PRESUP.", new Font(fontHead)));
                        PdfPCell hvto = new PdfPCell(new Phrase("VENCIMIENTO", new Font(fontHead)));
                        table.AddCell(hnro);
                        table.AddCell(hpresup);
                        table.AddCell(hempresa);
                        table.AddCell(hdetalle);
                        table.AddCell(himporte);
                        table.AddCell(hestado);
                        table.AddCell(hfechaPresup);
                        table.AddCell(hvto);


                        while (datos.Read())
                        {
                            String numeroOrden = datos["nroOrdenPrestacionServicio"].ToString();
                            String numeroPresup = datos["nroPresupuesto"].ToString();
                            String detalle = datos["detalle"].ToString();
                            String razon = datos["razonSocial"].ToString();
                            String importe = datos["importe"].ToString();
                            String estado = datos["descripcionEstado"].ToString();
                            String fechaOrden = datos["fechaOrden"].ToString();
                            String vto = datos["fechaValidez"].ToString();

                            PdfPCell corden = new PdfPCell(new Phrase(numeroOrden, new Font(fontBody)));
                            PdfPCell cpresup = new PdfPCell(new Phrase(numeroPresup, new Font(fontBody)));
                            PdfPCell cdetalle = new PdfPCell(new Phrase(detalle, new Font(fontBody)));
                            PdfPCell crazon = new PdfPCell(new Phrase(razon, new Font(fontBody)));
                            PdfPCell cimporte = new PdfPCell(new Phrase("$ " + importe, new Font(fontBody)));
                            PdfPCell cestado = new PdfPCell(new Phrase(estado, new Font(fontBody)));
                            PdfPCell cfechapresup = new PdfPCell(new Phrase(fechaOrden, new Font(fontBody)));
                            PdfPCell cvto = new PdfPCell(new Phrase(vto, new Font(fontBody)));



                            Label15.Text = DropDownList1.SelectedItem.ToString();
                            DateTime entre = Convert.ToDateTime(fechaOrden);




                            if (((desde <= entre) && (entre <= hasta))&&(Label15.Text.Equals(razon)))
                            {
                               
                                    table.AddCell(corden);
                                    table.AddCell(cpresup);
                                    table.AddCell(crazon);
                                    table.AddCell(cdetalle);
                                    table.AddCell(cimporte);
                                    table.AddCell(cestado);
                                    table.AddCell(cfechapresup);
                                    table.AddCell(cvto);
                                    presup = presup + 1;
                                    int imp = Convert.ToInt32(importe);
                                    total = total + imp;
                                
                            }


                        }

                        doc.Add(table);
                        doc.Add(new Paragraph("  "));
                        doc.Add(new Paragraph("  "));
                        doc.Add(new Paragraph("TOTAL DE ORDENES: " + presup));
                        doc.Add(new Paragraph("IMPORTE TOTAL: $" + total));
                        doc.Close();

                        Response.ContentType = "application/pdf";
                        Response.AddHeader("Content-Disposition", "attachment;filename=ORDENESPRESTACION.pdf");
                        Response.TransmitFile(Server.MapPath("~/") + "ORDENESPRESTACION.pdf");



                    }
                }
            }
            // Cierre por Empresa y fecha



        }



    }

    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        if (Label13.Text.Equals("desde"))
        {
            String date = Calendar1.SelectedDate.ToString("dd/MM/yyyy");
            TextBox2.Text = date;
            Calendar1.Visible = false;
            Button7.Enabled = true;

        }

        if (Label13.Text.Equals("hasta"))
        {

            String date = Calendar1.SelectedDate.ToString("dd/MM/yyyy");
            TextBox3.Text = date;
            Calendar1.Visible = false;

        }
    }
    protected void Calendar2_SelectionChanged(object sender, EventArgs e)
    {
        if (Label13.Text.Equals("desde"))
        {
            String date = Calendar2.SelectedDate.ToString("dd/MM/yyyy");
            TextBox2.Text = date;
            Calendar2.Visible = false;

        }

        if (Label13.Text.Equals("hasta"))
        {

            String date = Calendar2.SelectedDate.ToString("dd/MM/yyyy");
            TextBox3.Text = date;
            Calendar2.Visible = false;

        }
    }
    protected void Button6_Click(object sender, EventArgs e)
    {
        Calendar1.Visible = true;
        Label13.Text = "desde";
        Label14.Text = "";


    }
    protected void Button7_Click(object sender, EventArgs e)
    {
        Calendar2.Visible = true;
        Label13.Text = "hasta";
        Label14.Text = "";
    }


    protected void Button8_Click(object sender, EventArgs e)
    {
        Response.Redirect("frmPrestacionDeServicios.aspx");
    }
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (CheckBox1.Checked == true)
            Button6.Enabled = true;

        else
        {
            Calendar1.Visible = false;
            Calendar2.Visible = false;
            Button6.Enabled = false;
            Button7.Enabled = false;
            TextBox2.Text = "";
            TextBox3.Text = "";
            Label14.Text = "";
        
        }

       
    }

    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
    {
        if (CheckBox2.Checked == true)
            DropDownList1.Enabled = true;
        else
            DropDownList1.Enabled = false;

    }
}